<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
		xmlns:gml="http://www.opengis.net/gml" 
		xmlns:ogr="http://ogr.maptools.org/"
		xmlns:gx="http://www.google.com/kml/ext/2.2"
		xmlns:="http://www.opengis.net/kml/2.2">
		
<xsl:output method="xml" indent="yes" />
<xsl:template match="/ogr:FeatureCollection">

	<xsl:for-each select='gml:featureMember/ogr:NUTS_RG_60M_2006' >
		<xsl:result-document href="{concat('id/',ogr:NUTS_ID,'_geometry.kml')}">
			<kml>
			<Document>
				<xsl:apply-templates select="ogr:geometryProperty">
					<xsl:with-param name="nuts_id" select="ogr:NUTS_ID" />
				</xsl:apply-templates>			
			</Document>
			</kml>
		</xsl:result-document>
	</xsl:for-each>

</xsl:template>

<xsl:template match="ogr:geometryProperty">
	<xsl:param name="nuts_id" />	
	<Placemark>
		<name><xsl:value-of select="$nuts_id"/> Geometry</name>
		<ExtendedData xmlns:dc="http://purl.org/dc/elements/1.1/">
			<ogr:NUTS_ID><xsl:value-of select="$nuts_id"/></ogr:NUTS_ID>
			<dc:rights>© EuroGeographics for the administrative boundaries.</dc:rights>
		</ExtendedData>
		<xsl:apply-templates select="child::node()"/>
	</Placemark>
</xsl:template>

<xsl:template match="gml:MultiPolygon|gml:MultiSurface">
	<MultiGeometry>
		<xsl:apply-templates select="descendant::gml:Polygon|descendant::gml:Surface|descendant::gml:LineString|descendant::gml:Curve"/>
	</MultiGeometry>
</xsl:template>

<xsl:template name="polygon" match="gml:Polygon|gml:Surface">
	<Polygon>
		<xsl:apply-templates select="gml:interior|gml:innerBoundaryIs|gml:exterior|gml:outerBoundaryIs"/>
	</Polygon>
</xsl:template>

<xsl:template match="gml:interior|gml:innerBoundaryIs">
	<innerBoundaryIs>
		<xsl:apply-templates select="child::gml:LinearRing"/>
	</innerBoundaryIs>
</xsl:template>

<xsl:template match="gml:exterior|gml:outerBoundaryIs">
	<outerBoundaryIs>
		<xsl:apply-templates select="child::gml:LinearRing"/>
	</outerBoundaryIs>
</xsl:template>

<xsl:template match="gml:LinearRing">
	<LinearRing>
		<xsl:apply-templates select="child::gml:posList|child::gml:coordinates"/>
	</LinearRing>
</xsl:template>

<xsl:template match="gml:LineString|gml:Curve">	
	<LineString>
		<xsl:apply-templates select="child::gml:posList|child::gml:coordinates"/>
	</LineString>
</xsl:template>

<xsl:template match="gml:posList|gml:coordinates">
	<coordinates>	
		<xsl:value-of select="self::node()"/>
	</coordinates>
</xsl:template>

</xsl:stylesheet>